package net.sharpf2.api.util.web;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

public class Hiscore {

    private final String address = "http://hiscore.runescape.com/index_lite.ws?player=";
    public final String[] skillNames = { "Total", "Attack", "Defence",
            "Strength", "Constitution", "Range", "Prayer", "Magic", "Cooking",
            "Woodcutting", "Fletching", "Fishing", "Firemaking", "Crafting",
            "Smithing", "Mining", "Herblore", "Agility", "Thieving", "Slayer",
            "Farming", "Runecrafting", "Hunter", "Construction", "Summoning",
            "Dungeoneering" };

        /**
         * Looksup an player
         * @param   username    Username.
         * @return  Player.
         */
    public Player lookUp(String username) {
        try {
            Map<String, Integer> skillLvlMap = new HashMap<String, Integer>();
            Map<String, Integer> skillXpMap = new HashMap<String, Integer>();
            Map<String, Integer> skillRankMap = new HashMap<String, Integer>();

            URL url = new URL(address + username.replace(" ", "+"));
            BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()));
            String[] html;
            for (int i = 0; i < 26; i++) {
                html = br.readLine().split(",");
                skillLvlMap.put(skillNames[i], Integer.parseInt(html[1]));
                skillXpMap.put(skillNames[i], Integer.parseInt(html[2]));
                skillRankMap.put(skillNames[i], Integer.parseInt(html[0]));
                //System.out.println(skillNames[i] + " lvl: " + html[1] + " xp: " + html[2] + " rank: " + html[0]);
            }
            br.close();
            return new Player(username, skillLvlMap, skillXpMap, skillRankMap);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return null;
    }

    public class Player {

        private Map<String, Integer> skillLvlMap = new HashMap<String, Integer>();
        private Map<String, Integer> skillXpMap = new HashMap<String, Integer>();
        private Map<String, Integer> skillRankMap = new HashMap<String, Integer>();
        private String name;

        /**
         * Player
         * @param name  Username.
         * @param lvl   Level.
         * @param xp    Experience.
         * @param rank  Rank.
         */
        public Player(String name, Map lvl, Map xp, Map rank) {
            this.name = name;
            this.skillLvlMap = lvl;
            this.skillXpMap = xp;
            this.skillRankMap = rank;
        }

        /**
         * Gets the Player's name.
         * @return Username.
         */
        public String getName() {
            return name;
        }

        /**
         * Gets the Player's level in a specified skill.
         * @param   name    Skill name.
         * @return  Skill level.
         */
        public int getSkillLvl(String name) {
            return skillLvlMap.get(name);
        }

        /**
         * Gets the Player's XP in a specified skill.
         * @param   name    Skill name.
         * @return  Skill XP.
         */
        public int getSkillXp(String name) {
            return skillXpMap.get(name);
        }

        /**
         * Gets the Player's rank in a specified skill.
         * @param   name    Skill name.
         * @return  Skill rank.
         */
        public int getSkillRank(String name) {
            return skillRankMap.get(name);
        }

    }

}